#include "eventview.h"
#include "ui_eventview.h"
#include <optra/common/logevent.h>

#include <QListWidgetItem>

EventView::EventView(QWidget *parent) :
    GuiWindow(parent),
    ui(new Ui::EventView)
{
    ui->setupUi(this);
}

EventView::~EventView()
{
    delete ui;
}

void EventView::addEvent(Optra::LogEvent* ev)
{
    QStringList message;
    message << "[";

    QListWidgetItem* t = new QListWidgetItem;
    QFont font = t->font();

    switch(ev->depth()){
    case Optra::LogEvent::error:
        message << "ERROR   ";
        t->setBackground(Qt::red);
        t->setForeground(Qt::black);
        font.setBold(true);
        break;
    case Optra::LogEvent::critical:
        message << "CRITICAL";
        t->setForeground(Qt::red);
        break;
    case Optra::LogEvent::warning:
        message << "Warning ";
        font.setBold(true);        
        break;
    case Optra::LogEvent::info:
        message << "Info    ";
      //  t->setBackground(Qt::red);
        t->setForeground(Qt::black);
        font.setBold(true);
        break;
    case Optra::LogEvent::debug:
        message << "debug   ";
        t->setForeground(Qt::gray);
        break;
    case Optra::LogEvent::trace:
        message << "trace   ";
        t->setForeground(Qt::gray);
        font.setItalic(true);
        break;
    }
    message << "] ";
    if (ev->sender()){
        message << ev->sender()->zoneInfo()->name();
        message << " : ";
    }
    message << QTime::currentTime().toString() << " : " << ev->log();


    QString msg = message.join("");
    qDebug() << msg;
    t->setText(msg);
    t->setFont(font);
    ui->listWidget->addItem(t);
    ui->listWidget->scrollToBottom();
}

void EventView::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
